﻿using DbContext;
using Microsoft.EntityFrameworkCore;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using SmartPlant.Core;
using SmartPlant.Core.Mvvm;
using SmartPlant.Model;
using SmartPlant.Model.Biz;
using SmartPlant.Model.DbContext;
using SmartPlant.Services;
using SmartPlant.Services.Interfaces;
using SmartPlant.WinCore;
using SmartPlant.WinCore.Helper;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Charting;
using Telerik.Windows.Controls.Map;
using Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx;
using Telerik.Windows.Documents.Spreadsheet.Model;
using Telerik.Windows.Persistence.Core;
using Unity;
using Line = SmartPlant.Model.DbContext.Line;

namespace SmartPlant.Modules.Base.ViewModels
{
    public class PdLevelEditViewModel : RegionViewModelBase
    {


        private ObservableCollection<DropDownListObj> _list_pdFluidPhase;

        public ObservableCollection<DropDownListObj> list_pdFluidPhase
        {
            get { return _list_pdFluidPhase; }
            set
            {
                SetProperty(ref _list_pdFluidPhase, value);
            }
        }


        private ObservableCollection<DropDownListObj> _list_pdFlowUid;

        public ObservableCollection<DropDownListObj> list_pdFlowUid
        {
            get { return _list_pdFlowUid; }
            set
            {
                SetProperty(ref _list_pdFlowUid, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdDensUid;

        public ObservableCollection<DropDownListObj> list_PdDensUid
        {
            get { return _list_PdDensUid; }
            set
            {
                SetProperty(ref _list_PdDensUid, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdPressUid;
        public ObservableCollection<DropDownListObj> list_PdPressUid
        {
            get { return _list_PdPressUid; }
            set
            {
                SetProperty(ref _list_PdPressUid, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdTempUid;
        public ObservableCollection<DropDownListObj> list_PdTempUid
        {
            get { return _list_PdTempUid; }
            set
            {
                SetProperty(ref _list_PdTempUid, value);
            }
        }




        private ObservableCollection<DropDownListObj> _list_PdPressDesUid;
        public ObservableCollection<DropDownListObj> list_PdPressDesUid
        {
            get { return _list_PdPressDesUid; }
            set
            {
                SetProperty(ref _list_PdPressDesUid, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdPressDesMinUid;
        public ObservableCollection<DropDownListObj> list_PdPressDesMinUid
        {
            get { return _list_PdPressDesMinUid; }
            set
            {
                SetProperty(ref _list_PdPressDesMinUid, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdDesignTempUid;
        public ObservableCollection<DropDownListObj> list_PdDesignTempUid
        {
            get { return _list_PdDesignTempUid; }
            set
            {
                SetProperty(ref _list_PdDesignTempUid, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdVapPressUflg;
        public ObservableCollection<DropDownListObj> list_PdVapPressUflg
        {
            get { return _list_PdVapPressUflg; }
            set
            {
                SetProperty(ref _list_PdVapPressUflg, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdPressUflg;
        public ObservableCollection<DropDownListObj> list_PdPressUflg
        {
            get { return _list_PdPressUflg; }
            set
            {
                SetProperty(ref _list_PdPressUflg, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdPressDesUflg;
        public ObservableCollection<DropDownListObj> list_PdPressDesUflg
        {
            get { return _list_PdPressDesUflg; }
            set
            {
                SetProperty(ref _list_PdPressDesUflg, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdFlowUflg;
        public ObservableCollection<DropDownListObj> list_PdFlowUflg
        {
            get { return _list_PdFlowUflg; }
            set
            {
                SetProperty(ref _list_PdFlowUflg, value);
            }
        }

      

       
        private ObservableCollection<DropDownListObj> _list_PdPressDownUflg;
        public ObservableCollection<DropDownListObj> list_PdPressDownUflg
        {
            get { return _list_PdPressDownUflg; }
            set
            {
                SetProperty(ref _list_PdPressDownUflg, value);
            }
        }

        private ObservableCollection<DropDownListObj> _list_PdPressDesMinUflg;
        public ObservableCollection<DropDownListObj> list_PdPressDesMinUflg
        {
            get { return _list_PdPressDesMinUflg; }
            set
            {
                SetProperty(ref _list_PdPressDesMinUflg, value);
            }
        }
       
        private ObservableCollection<DropDownListObj> _list_LINE_ID;
        public ObservableCollection<DropDownListObj> list_LINE_ID
        {
            get { return _list_LINE_ID; }
            set
            {
                SetProperty(ref _list_LINE_ID, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdColored;
        public ObservableCollection<DropDownListObj> list_PdColored
        {
            get { return _list_PdColored; }
            set
            {
                SetProperty(ref _list_PdColored, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdErosive;
        public ObservableCollection<DropDownListObj> list_PdErosive
        {
            get { return _list_PdErosive; }
            set
            {
                SetProperty(ref _list_PdErosive, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdLowerDensityUid;
        public ObservableCollection<DropDownListObj> list_PdLowerDensityUid
        {
            get { return _list_PdLowerDensityUid; }
            set
            {
                SetProperty(ref _list_PdLowerDensityUid, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdFluidPhaseLow;
        public ObservableCollection<DropDownListObj> list_PdFluidPhaseLow
        {
            get { return _list_PdFluidPhaseLow; }
            set
            {
                SetProperty(ref _list_PdFluidPhaseLow, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdCorrosive;
        public ObservableCollection<DropDownListObj> list_PdCorrosive
        {
            get { return _list_PdCorrosive; }
            set
            {
                SetProperty(ref _list_PdCorrosive, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdSourceLow;
        public ObservableCollection<DropDownListObj> list_PdSourceLow
        {
            get { return _list_PdSourceLow; }
            set
            {
                SetProperty(ref _list_PdSourceLow, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdToxic;
        public ObservableCollection<DropDownListObj> list_PdToxic
        {
            get { return _list_PdToxic; }
            set
            {
                SetProperty(ref _list_PdToxic, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_Case_Id;
        public ObservableCollection<DropDownListObj> list_Case_Id
        {
            get { return _list_Case_Id; }
            set
            {
                SetProperty(ref _list_Case_Id, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdFluidStat;
        public ObservableCollection<DropDownListObj> list_PdFluidStat
        {
            get { return _list_PdFluidStat; }
            set
            {
                SetProperty(ref _list_PdFluidStat, value);
            }
        }
        private ObservableCollection<DropDownListObj> _list_PdSource;
        public ObservableCollection<DropDownListObj> list_PdSource
        {
            get { return _list_PdSource; }
            set
            {
                SetProperty(ref _list_PdSource, value);
            }
        }
        private LevelInstrument _LevelInstrument;
        public LevelInstrument LevelInstrument
        {
            get => _LevelInstrument;
            set => SetProperty(ref _LevelInstrument, value);
        }
        private Flow _Flowdata;
        public Flow Flowdata
        {
            get => _Flowdata;
            set => SetProperty(ref _Flowdata, value);
        }

        private PdGeneral _data;

        public PdGeneral data
        {
            get { return _data; }
            set
            {
                if (SetProperty(ref _data, value))
                {
                    if (_data != null)
                    {
                        _data.PropertyChanged += (sender, args) =>
                        {
                            if (args.PropertyName == nameof(data.PdFluidPhase))
                            {
                                UpdateVisibility();
                            }
                        };
                    }
                }
            }
        }
        private void UpdateVisibility()
        {
            if (data != null)
            {
                SNotVisible = data.PdFluidPhase == "S";
                CombineNotVisible = (data.PdFluidPhase == "P" || data.PdFluidPhase == "L"|| data.PdFluidPhase == "W" || data.PdFluidPhase == "S" || data.PdFluidPhase == "G");

            }
        }
        private string _LineSize;
        public string LineSize
        {
            get { return _LineSize; }
            set { SetProperty(ref _LineSize, value); }
        }
        private string _LineUom;
        public string LineUom
        {
            get { return _LineUom; }
            set { SetProperty(ref _LineUom, value); }
        }
        private string _LineSche;
        public string LineSche
        {
            get { return _LineSche; }
            set { SetProperty(ref _LineSche, value); }
        }
        private SmartPlant.Model.DbContext.Line _Linedata2;
        public SmartPlant.Model.DbContext.Line Linedata2
        {
            get { return _Linedata2; }
            set
            {
                SetProperty(ref _Linedata2, value);
            }
        }
        private async void UpdateLinedata()
        {
            Linedata2 = await LineServ.GetById(Componentdata.LINE_ID);
            LineSize = Linedata2.LineSize;
            LineUom = Linedata2.LineUom;
            LineSche = Linedata2.LineSched;
        }
        private bool _SNotVisible;
        public bool SNotVisible
        {
            get => _SNotVisible;
            set => SetProperty(ref _SNotVisible, value);
        }
        private bool _CombineNotVisible;
        public bool CombineNotVisible
        {
            get => _CombineNotVisible;
            set => SetProperty(ref _CombineNotVisible, value);
        }

        private bool _isControlLevelVisible;
        public bool IsControlLevelVisible
        {
            get => _isControlLevelVisible;
            set => SetProperty(ref _isControlLevelVisible, value);
        }



        private Instruments _Componentdata;

        public Instruments Componentdata
        {
            get { return _Componentdata; }
            set
            {
                if (SetProperty(ref _Componentdata, value))
                {
                    if (_Componentdata != null)
                    {
                        _Componentdata.PropertyChanged += (sender, args) =>
                        {
                            if (args.PropertyName == nameof(Componentdata.LINE_ID))
                            {
                                UpdateLinedata();
                            }
                        };
                    };
                }
            }
        }

        private SmartPlant.Model.DbContext.Line _Linedata;

        public SmartPlant.Model.DbContext.Line Linedata
        {
            get { return _Linedata; }
            set
            {
                SetProperty(ref _Linedata, value);
            }
        }

        private DelegateCommand<object> _AddCaseCommand;

        public ICommand AddCaseCommand
        {
            get
            {
                if (_AddCaseCommand != null) return _AddCaseCommand;
                _AddCaseCommand = new DelegateCommand<object>(onSaveCommand);
                return _AddCaseCommand;
            }
        }
        private DelegateCommand<object> _DeleteCaseCommand;

        public ICommand DeleteCaseCommand
        {
            get
            {
                if (_DeleteCaseCommand != null) return _DeleteCaseCommand;
                _DeleteCaseCommand = new DelegateCommand<object>(onSaveCommand);
                return _DeleteCaseCommand;
            }
        }
        private DelegateCommand<object> _ChangeCaseCommand;

        public ICommand ChangeCaseCommand
        {
            get
            {
                if (_ChangeCaseCommand != null) return _ChangeCaseCommand;
                _ChangeCaseCommand = new DelegateCommand<object>(onSaveCommand);
                return _ChangeCaseCommand;
            }
        }
        private DelegateCommand<object> _PrintCommand;

        public ICommand PrintCommand
        {
            get
            {
                if (_PrintCommand != null) return _PrintCommand;
                _PrintCommand = new DelegateCommand<object>(onSaveCommand);
                return _PrintCommand;
            }
        }

        private DelegateCommand<object> _CopyFromCommand;

        public ICommand CopyFromCommand
        {
            get
            {
                if (_CopyFromCommand != null) return _CopyFromCommand;
                _CopyFromCommand = new DelegateCommand<object>(onSaveCommand);
                return _CopyFromCommand;
            }
        }
        private DelegateCommand<object> _RevionManagerCommand;

        public ICommand RevionManagerCommand
        {
            get
            {
                if (_RevionManagerCommand != null) return _RevionManagerCommand;
                _RevionManagerCommand = new DelegateCommand<object>(onSaveCommand);
                return _RevionManagerCommand;
            }
        }
        private DelegateCommand<object> _LineProcessDataCommand;

        public ICommand LineProcessDataCommand
        {
            get
            {
                if (_LineProcessDataCommand != null) return _LineProcessDataCommand;
                _LineProcessDataCommand = new DelegateCommand<object>(onSaveCommand);
                return _LineProcessDataCommand;
            }
        }
        private DelegateCommand<object> _SaveAsCommand;

        public ICommand SaveAsCommand
        {
            get
            {
                if (_SaveAsCommand != null) return _SaveAsCommand;
                _SaveAsCommand = new DelegateCommand<object>(onSaveCommand);
                return _SaveAsCommand;
            }
        }


        private DelegateCommand<object> _SaveCommand;

        public ICommand SaveCommand
        {
            get
            {
                if (_SaveCommand != null) return _SaveCommand;
                _SaveCommand = new DelegateCommand<object>(onSaveCommand);
                return _SaveCommand;
            }
        }
        private bool _IsUpdating;

        public bool IsUpdating
        {
            get { return _IsUpdating; }
            set
            {
                SetProperty(ref _IsUpdating, value);
            }
        }

        public override bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false;
        }

        private readonly IEventAggregator _eventAggregator;

        private string ProjectID { get; set; }
        private string CmpntOrLineID { get; set; }
        private string PdTitle { get; set; }
        private ObjectDefId ObjectDefId { get; set; }

        private readonly IPDGeneralService pdServ;
        private readonly IInstrumentService TagServ;
        private readonly ILineService LineServ;
        private readonly ILevelInstrumentService levelInstrumentService;
        private readonly ICodeListService codelistServ;
        private readonly IFlowService flowServ;

        public PdLevelEditViewModel(
            IRegionManager regionManager,
            IDialogService dialog,
            IUnityContainer unityContainer,
            IEventAggregator eventAggregator) : base(regionManager, unityContainer, dialog)
        {
            TagServ = unityContainer.Resolve<InstrumentService>();
            LineServ = unityContainer.Resolve<LineService>();
            pdServ = unityContainer.Resolve<PDGeneralService>();
            codelistServ = unityContainer.Resolve<CodeListService>();
            flowServ = unityContainer.Resolve<FlowService>();
            levelInstrumentService = unityContainer.Resolve<LevelInstrumentService>();
            ProjectID = GlobalObject.CurrentProject.ProjId.ToString();
            //list_pdFluidPhase = GlobalObject.PropertyDropDownList.GetValueOrNull(GlobalObject.CurrentProject.ProjId.ToString() + "_" + 1312);
            list_pdFluidPhase = new ObservableCollection<DropDownListObj>();
            list_pdFlowUid = new ObservableCollection<DropDownListObj>();
            list_PdDensUid = new ObservableCollection<DropDownListObj>();
            list_PdPressUid = new ObservableCollection<DropDownListObj>();
            list_PdTempUid = new ObservableCollection<DropDownListObj>();
            list_PdPressDesUid = new ObservableCollection<DropDownListObj>();
            list_PdPressDesMinUid = new ObservableCollection<DropDownListObj>();
            list_PdDesignTempUid = new ObservableCollection<DropDownListObj>();
            list_PdVapPressUflg = new ObservableCollection<DropDownListObj>();
            list_PdPressUflg = new ObservableCollection<DropDownListObj>();
            list_PdPressDesUflg = new ObservableCollection<DropDownListObj>();
            list_PdFlowUflg = new ObservableCollection<DropDownListObj>();

            list_PdPressDesMinUflg = new ObservableCollection<DropDownListObj>();
            list_PdColored = new ObservableCollection<DropDownListObj>();
            list_LINE_ID = new ObservableCollection<DropDownListObj>();
            list_PdToxic = new ObservableCollection<DropDownListObj>();
            list_PdCorrosive = new ObservableCollection<DropDownListObj>();
            list_PdErosive = new ObservableCollection<DropDownListObj>();
            list_Case_Id = new ObservableCollection<DropDownListObj>();
            list_PdSource = new ObservableCollection<DropDownListObj>();
            list_PdFluidStat = new ObservableCollection<DropDownListObj>();
            
            list_PdLowerDensityUid = new ObservableCollection<DropDownListObj>();
            list_PdFluidPhaseLow =  new ObservableCollection<DropDownListObj>();
           
            list_PdSourceLow = new ObservableCollection<DropDownListObj>();
        }

        protected override async void OnInitPage(object obj)
        {
            var Ids = "1310,1294,1351,1394,1431,1426,1422,1283,1342,1340,1297,1421,1350,1341,1309,1339,1346,13137,13138,11121,11096,11125";
            await codelistServ.GetDropDownListsByAttrAsync(GlobalObject.CurrentProject.ProjId.ToString(), Ids.Split(',').ToList());

            list_pdFluidPhase.AddRange(await codelistServ.GetDropDownListByDefAsync("E2010", "enum"));
            list_PdFluidPhaseLow.AddRange(await codelistServ.GetDropDownListByAttrAsync(11096));
            list_PdSourceLow.AddRange(await codelistServ.GetDropDownListByAttrAsync(11125));
            list_PdLowerDensityUid.AddRange(await codelistServ.GetDropDownListByAttrAsync(11121));
            list_pdFlowUid.AddRange(await codelistServ.GetDropDownListByAttrAsync(1310));
            list_PdDensUid.AddRange(await codelistServ.GetDropDownListByAttrAsync(1294));
            list_PdPressUid.AddRange(await codelistServ.GetDropDownListByAttrAsync(1351));
            list_PdTempUid.AddRange(await codelistServ.GetDropDownListByAttrAsync(1394));
          
            list_PdPressDesUid.AddRange(await codelistServ.GetDropDownListByAttrAsync(1342));
            list_PdPressDesMinUid.AddRange(await codelistServ.GetDropDownListByAttrAsync(1340));
            list_PdDesignTempUid.AddRange(await codelistServ.GetDropDownListByAttrAsync(1297));
            list_PdVapPressUflg.AddRange(await codelistServ.GetDropDownListByAttrAsync(1421));
            list_PdPressUflg.AddRange(await codelistServ.GetDropDownListByAttrAsync(1350));
            list_PdPressDesUflg.AddRange(await codelistServ.GetDropDownListByAttrAsync(1341));
            list_PdFlowUflg.AddRange(await codelistServ.GetDropDownListByAttrAsync(1309));
      
            list_PdPressDesMinUflg.AddRange(await codelistServ.GetDropDownListByAttrAsync(1339));

            list_LINE_ID.AddRange(await codelistServ.GetDropDownListByDefAsync(GlobalObject.lookupDic[nameof(Line)]));
            list_PdErosive.AddRange(await codelistServ.GetDropDownListByAttrAsync(1302));
            list_PdToxic.AddRange(await codelistServ.GetDropDownListByAttrAsync(1396));
            list_PdCorrosive.AddRange(await codelistServ.GetDropDownListByAttrAsync(1277));
            list_PdColored.AddRange(await codelistServ.GetDropDownListByAttrAsync(1266));
            list_Case_Id.AddRange(await codelistServ.GetDropDownListByAttrAsync(1233));
            list_PdFluidStat.AddRange(await codelistServ.GetDropDownListByAttrAsync(1313));
            list_PdSource.AddRange(await codelistServ.GetDropDownListByAttrAsync(1379));







        }
        public override async void OnNavigatedTo(NavigationContext navigationContext)
        {
            IsUpdating = true;
            if (navigationContext.Parameters.ContainsKey(DialogParameterKeys.Id))
                CmpntOrLineID = navigationContext.Parameters[DialogParameterKeys.Id].ToString();
            if (navigationContext.Parameters.ContainsKey(DialogParameterKeys.ObjectType))
                ObjectDefId = (ObjectDefId)navigationContext.Parameters[DialogParameterKeys.ObjectType];
            if (navigationContext.Parameters.ContainsKey(DialogParameterKeys.Title))
                PdTitle = navigationContext.Parameters[DialogParameterKeys.Title].ToString();
                if (decimal.TryParse(CmpntOrLineID, out decimal cmpntId))
            {
                if (ObjectDefId == ObjectDefId.component)
                {
                    Title.TitleText = PdTitle;
                    data = await pdServ.GetProcessDataAsync(GlobalObject.CurrentProject.ProjId, ObjectDefId.component, cmpntId);
                    //LevelInstrument = 
                    Componentdata = await TagServ.GetInstrumentAsync(GlobalObject.CurrentProject.ProjId, cmpntId.ToString());
                    Linedata = await LineServ.GetById(Componentdata.LINE_ID);
                    LevelInstrument  = await levelInstrumentService.GetById(Componentdata.CMPNT_ID);
                    LineSize = Linedata.LineSize;
                    LineUom = Linedata.LineUom;
                    LineSche = Linedata.LineSched;

                }
                else if (ObjectDefId == ObjectDefId.line)
                {
                    Title.TitleText = PdTitle;
                    data = await pdServ.GetProcessDataAsync(GlobalObject.CurrentProject.ProjId, ObjectDefId.line, cmpntId);

                }
            }
            if (data != null)
            {

                switch (data.PdFluidPhase)
                {
                    case "L":
                        break;
                    default:
                        break;
                }
            }
                IsUpdating = false;
            RaisePropertyChanged(nameof(Title));
            RaisePropertyChanged(nameof(IsControlLevelVisible));

        }


        #region command
        private async void onSaveCommand(object obj)
        {
            var x = data.PdFluidName;
            var res = await pdServ.UpdateProcessDataAsync(Convert.ToDecimal(ProjectID), data);
            var res2 = await TagServ.UpdateInstrumentAsync(Convert.ToDecimal(ProjectID), Componentdata);
            var res3 = await levelInstrumentService.UpdateLevelInstrumentAsync(Convert.ToDecimal(ProjectID), LevelInstrument);

            if (res && res2 && res3 )
            {
                Dialog.ShowMessage("保存成功！");
            }
            {

            }
        }
        #endregion
    }
}
